// memmove模拟实现

#include <iostream>
#include <assert.h>

void* memmove_self(void* destination, const void* source, size_t num)
{
    void* start = destination;
    assert(destination && source);
    if(destination < source) // 从前往后覆盖
    {
        while(num--)
        {
            *(char*)destination = *(char*)source;
            destination = (char*)destination + 1;
            source = (char*)source + 1;
        }
    }
    else // 从后往前覆盖
    {
        while(num--)
        {
            *((char*)destination + num) = *((char*)source + num);
        }
    }
    return start;
}

int main()
{
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    printf("%s\n", memmove_self(arr + 3, arr, 16));
    for(int i = 0; i < sizeof(arr) / sizeof(int); ++i)
    {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}
